import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LineChart extends JFrame {
    private static final long serialVersionUID=1L;

    public LineChart(String applicationTitle,String chartTitle,ArrayList<ArrayList<Double>> data,String[] legends) {
        super(applicationTitle);
        JFreeChart lineChart=ChartFactory.createXYLineChart(
                chartTitle,
                "Index",
                "Value",
                createDataset(data,legends),
                PlotOrientation.VERTICAL,
                true,true,false);

        ChartPanel chartPanel=new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(560,367));
        final XYPlot plot=lineChart.getXYPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.black);
        plot.setRangeGridlinePaint(Color.black);

        final NumberAxis rangeAxis=(NumberAxis)plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane(chartPanel);
    }

    private XYSeriesCollection createDataset(ArrayList<ArrayList<Double>> data,String[] legends) {
        XYSeriesCollection dataset=new XYSeriesCollection();
        for(int i=0;i<data.size();i++) {
            XYSeries series=new XYSeries(legends[i]);
            for(int j=0;j<data.get(i).size();j++) {
                series.add(j,data.get(i).get(j));
            }
            dataset.addSeries(series);
        }
        return dataset;
    }


}
